package com.bao123.email.service.impl;

import com.bao123.email.service.EmailService;
import com.bao123.email.vo.MailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.Date;

/**
 * 邮件服务类实现类
 * @author lbw
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendMimeMail(MailVO mailVO) {
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
            messageHelper.setFrom(mailVO.getFrom());
            messageHelper.setTo(mailVO.getTo());
            messageHelper.setSubject(mailVO.getSubject());
            messageHelper.setText(mailVO.getText());
            messageHelper.setSentDate(new Date());
            mailSender.send(messageHelper.getMimeMessage());
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
